<template>
  <BasicTable
    @register="registerTable"
    :rowSelection="{ type: 'checkbox', selectedRowKeys: checkedKeys, onChange: onSelectChange }"
  >
    <template #form-custom></template>
    <template #headerTop>
      <alert type="info" show-icon v-if="checkedKeys.length > 0">
        <template #message>
            <span>已选中{{ checkedKeys.length }}条记录(可跨页)</span>
            <a-button type="link" @click="checkedKeys = []" size="small">清空</a-button>
        </template>
      </alert>
    </template>
    <template #toolbar>
      <a-button type="primary" @click="handleBatchCodeGen"> 批量生成</a-button>
    </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
            {
              label: '生成',
              onClick: handleCodeGenSet.bind(null, record),
            }
          ]"
      />
    </template>

  </BasicTable>
  <code-gen-set-form-modal @register="openSetRegister"></code-gen-set-form-modal>
</template>
<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import {BasicTable, useTable , TableAction} from '/@/components/Table';
import {getBasicColumns, getFormConfig} from './generatorTableData';
import {fetchList} from "/@/api/development/generatorApi";
import CodeGenSetFormModal from "/@/views/development/generator/CodeGenSetFormModal.vue";
import {useModal} from "/@/components/Modal";
import { Alert } from 'ant-design-vue';
import { message } from 'ant-design-vue';

const [registerTable, {reload, getForm, setTableData, setPagination, setLoading}] = useTable({
  title: '代码生成',
  columns: getBasicColumns(),
  useSearchForm: true,
  formConfig: getFormConfig(),
  showTableSetting: true,
  rowKey: 'tableName',
  bordered: true,
  showIndexColumn: true,
  actionColumn: {
    width: 80,
    title: '操作',
    dataIndex: 'action',
    slots: {customRender: 'action'},
  },
  api: fetchList,
  fetchSetting: {
    pageField: 'current',
    // 每页显示多少条
    sizeField: 'pageSize',
    // 请求结果列表字段  支持 a.b.c
    listField: 'records',
    totalField: 'total',

  },
});
const [openSetRegister, { openModal: openSetModal }] = useModal();
const checkedKeys = ref<Array<string | number>>([]);

function handleCodeGenSet(record: Recordable){
  const { dsName } = getForm().getFieldsValue();
  openSetModal(true,{
    tableName: record.tableName,
    dsName: dsName
  })
}
function handleBatchCodeGen(){
  if (!checkedKeys || checkedKeys.value.length<2 || checkedKeys.value.length>10 ) {
    message.error('选中表数量不合法，数量最少2个或最多为10个');
    return false;
  }

  const { dsName } = getForm().getFieldsValue();

  openSetModal(true,{
    tableName:checkedKeys.value.join("-"),
    dsName: dsName
  })
}

function onSelectChange(selectedRowKeys: (string | number)[]) {
  console.log(selectedRowKeys.length);
  checkedKeys.value = selectedRowKeys;
}

onMounted(() => {
});


</script>
